import { getAccountApi, loginPwdApi, registerApi, getHomeApi } from "../../utils/serviceApiUrl.js";
import storageHelper from "../../utils/storageHelper";
const USER_INFO = "USER_INFO";
const HOME_DATA = "HOME_DATA";
const state = {
    userInfo: null,
    homeData: null
};

const getters = {
    userInfo(state) {
        if (state.userInfo) {
            return state.userInfo;
        } else {
            return storageHelper.get("jmp_client_userInfo");
        }
    },
    homeData(state) {
        if (state.homeData) {
            return state.homeData;
        } else {
            return storageHelper.get("jmp_client_homeData");
        }
    }
};

const actions = {
    getHomeData({ commit }, param) {
        getHomeApi(param).then((res) => {
            if (res.code == 0) {
                commit(HOME_DATA, res.rs);
                storageHelper.set("jmp_client_homeData", res.rs);
            }
        });
    },
    getAccountInfo({ commit }, param) {
        getAccountApi(param).then((res) => {
            if (res.code == 0) {
                commit(USER_INFO, res.rs);
                storageHelper.set("jmp_client_userInfo", res.rs);
            }
        });
    },
    loginSen({ commit }, param) {
        return new Promise((resolve) => {
            loginPwdApi(param).then((res) => {
                commit(USER_INFO, res.rs.user);
                storageHelper.set("jmp_client_userInfo", res.rs.user);
                storageHelper.set("jmp_client_token", res.rs.token);
                resolve(res);
            });
        });
    },
    // eslint-disable-next-line no-unused-vars
    register({ commit }, param) {
        return new Promise((resolve, reject) => {
            registerApi(param).then((res) => {
                if (res.code == 0) {
                    resolve(res);
                } else {
                    reject(res.messages);
                }
            });
        });
    }
};
const mutations = {
    [USER_INFO](state, data) {
        state.userInfo = data;
    },
    [HOME_DATA](state, data) {
        data.totalFil = 0;

        if (data.commissionTotalFil) {
            data.totalFil += data.commissionTotalFil;
        }
        if (data.incomeTotalFil) {
            data.totalFil += data.incomeTotalFil;
        }
        if (data.freeTotalFil) {
            data.totalFil += data.freeTotalFil;
        }
        if (data.lockTotalFil) {
            data.totalFil += data.lockTotalFil;
        }

        data.todayFil = 0;
        if (data.commissionTodayFil) {
            data.todayFil += data.commissionTodayFil;
        }
        if (data.incomeTodayFil) {
            data.todayFil += data.incomeTodayFil;
        }
        if (data.freeTodayFil) {
            data.todayFil += data.freeTodayFil;
        }
        if (data.lockTodayFil) {
            data.todayFil += data.lockTodayFil;
        }
        state.homeData = data;
    }
};

export default {
    state,
    getters,
    actions,
    mutations
};
